package com.hj.security.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.hj.security.R;
import com.hj.security.R.id;
import com.hj.security.R.layout;
import com.hj.security.adapter.MainUIAdapter;

public class MainActivity extends Activity implements OnItemClickListener{

	private SharedPreferences sp;
	private MainUIAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        sp = this.getSharedPreferences("config", Context.MODE_PRIVATE);
        GridView gridView = (GridView) findViewById(R.id.gv_main);
        adapter = new MainUIAdapter(this);
        gridView.setAdapter(adapter);
        
        gridView.setOnItemClickListener(this);
        gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, final View view,
					int position, long id) {
				if(position==0){
					AlertDialog.Builder builder = new Builder(MainActivity.this);
					builder.setTitle("设置");
					builder.setMessage("请输入要更改的名称");
					final EditText et = new EditText(MainActivity.this);
					et.setHint("新名称");
					builder.setView(et);
					builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							String name = et.getText().toString();
							if("".equals(name)){
								Toast.makeText(MainActivity.this, "输入内容不能为空", Toast.LENGTH_LONG).show();
							}else{
								Editor editor = sp.edit();
								editor.putString("lostName", name);
								editor.commit();
								
								TextView tv = (TextView) view.findViewById(R.id.tv_main_name);
								tv.setText(name);
								adapter.notifyDataSetChanged();
							}
						}
						
					});
					builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							
						}
					});
					builder.show();
				}
				return false;
			}
		});
    }
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		switch (position) {
		case 0:// 手机防盗

			break;
		case 1:// 通讯卫士

			break;
		case 2:// 软件管理
			Intent app_manager_intent = new Intent(this, AppManagerActivity.class);
			startActivity(app_manager_intent);
			break;
		case 3:// 流量管理
			Intent tmintent = new Intent(this, TrafficManagerActivity.class);
			startActivity(tmintent);
			break;
		case 4:// 任务管理
			Intent pmIntent = new Intent(this, ProcessManagerActivity.class);
			startActivity(pmIntent);
			break;
		case 5:// 手机杀毒

			break;
		case 6:// 系统优化
			Intent otTntent = new Intent(this, OptimizeActivity.class);
			startActivity(otTntent);
			break;
		case 7:// 高级工具
				Intent atoolIntent = new Intent(this, AToolActivity.class);
				startActivity(atoolIntent);
			break;
		case 8:// 设置中心
			Intent stIntent = new Intent(this, SettingActivity.class);
			startActivity(stIntent);
			break;
		case 9://

			break;

		default:
			break;
		}
		
	}


    
}
